package com.sun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.common.Result;
import com.sun.common.SystemCrmlog;
import com.sun.entity.DrugSupplier;
import com.sun.entity.Supplier;
import com.sun.service.DrugSupplierService;
import com.sun.service.SupplierService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Project: hospitalDrugInformationManagement
 * @Author: 86178
 * @Date: 2023/2/28 9:22
 * @FileName: SupplierController
 * @Description: ""
 */
@RestController
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private DrugSupplierService drugSupplierService;

    /**
     * 新增供应商
     *
     * @param supplier
     * @return
     */
    @SystemCrmlog(title = "药品信息管理模块", type = "INSERT", description = "新增供应商")
    @RequiresAuthentication
    @PostMapping
    public Result<String> save(@RequestBody Supplier supplier) {
        boolean save = supplierService.save(supplier);
        if (save) {
            return Result.success("新增供应商成功");
        } else {
            return Result.error("新增供应商失败");
        }
    }

    /**
     * 新增供应商的药品
     *
     * @param drugSupplier
     * @return
     */
    @SystemCrmlog(title = "药品信息管理模块", type = "INSERT", description = "新增供应商的药品")
    @RequiresAuthentication
    @PostMapping("/saveWithDrug")
    public Result<String> saveWithDrug(@RequestBody DrugSupplier drugSupplier) {
        LambdaQueryWrapper<DrugSupplier> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DrugSupplier::getDrugName, drugSupplier.getDrugName());
        lambdaQueryWrapper.eq(DrugSupplier::getSupplierId, drugSupplier.getSupplierId());
        DrugSupplier one = drugSupplierService.getOne(lambdaQueryWrapper);
        if (one != null) {
            return Result.error("该供应商已有该药品");
        }
        boolean save = drugSupplierService.save(drugSupplier);
        if (save) {
            return Result.success("新增供应商的药品成功");
        } else {
            return Result.error("新增供应商的药品失败");
        }
    }

    /**
     * 根据id删除供应商药品
     *
     * @param id
     * @return
     */
    @SystemCrmlog(title = "药品信息管理模块", type = "DELETE", description = "根据id删除供应商药品")
    @RequiresAuthentication
    @DeleteMapping("/deleteWithDrug/{id}")
    public Result<String> deleteWithDrug(@PathVariable Long id) {
        boolean remove = drugSupplierService.removeById(id);
        if (remove) {
            return Result.success("供应商药品信息删除成功");
        } else {
            return Result.error("供应商药品信息删除失败");
        }
    }

    /**
     * 根据id修改供应商信息
     *
     * @param drugSupplier
     * @return
     */
    @SystemCrmlog(title = "药品信息管理模块", type = "UPDATE", description = "根据id修改供应商信息")
    @RequiresAuthentication
    @PutMapping("/updateWithDrug")
    public Result<String> updateWithDrug(@RequestBody DrugSupplier drugSupplier) {
        drugSupplierService.updateById(drugSupplier);
        return Result.success("供应商药品信息修改成功");
    }

    /**
     * 分页查询供应商药品信息
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequiresAuthentication
    @GetMapping("/pageWithDrug")
    public Result<Page> pageWithDrug(int pageNum, int pageSize, String drugName, String supplierName) {
        Page<DrugSupplier> pageInfo = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DrugSupplier> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(drugName), DrugSupplier::getDrugName, drugName);
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(supplierName), DrugSupplier::getSupplierName, supplierName);
        lambdaQueryWrapper.orderByAsc(DrugSupplier::getPrice);
        drugSupplierService.page(pageInfo, lambdaQueryWrapper);
        return Result.success(pageInfo);
    }

    @RequiresAuthentication
    @GetMapping("/getSupplierList")
    public Result getSupplierList() {
        List<Supplier> list = supplierService.list();
        return Result.success(list);
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequiresAuthentication
    @GetMapping("/page")
    public Result<Page> page(int pageNum, int pageSize) {
        Page<Supplier> pageInfo = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Supplier> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(Supplier::getSupplierName);
        supplierService.page(pageInfo, lambdaQueryWrapper);
        return Result.success(pageInfo);
    }

    /**
     * 根据id删除供应商
     *
     * @param id
     * @return
     */
    @SystemCrmlog(title = "药品信息管理模块", type = "DELETE", description = "根据id删除供应商")
    @RequiresAuthentication
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Long id) {
        boolean remove = supplierService.remove(id);
        if (remove) {
            return Result.success("供应商信息删除成功");
        } else {
            return Result.error("供应商信息删除失败");
        }
    }

    /**
     * 根据id修改供应商信息
     * @param supplier
     * @return
     */
    @SystemCrmlog(title = "药品信息管理模块", type = "UPDATE", description = "根据id修改供应商信息")
    @RequiresAuthentication
    @PutMapping
    public Result<String> update(@RequestBody Supplier supplier){
        supplierService.updateById(supplier);
        return Result.success("供应商信息修改成功");
    }
}
